package com.example.rfidserver.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.rfidserver.R;
import com.example.rfidserver.dao.ConsumeDao;
import com.example.rfidserver.dao.TicketDao;
import com.example.rfidserver.dao.UserDao;
import com.example.rfidserver.pojo.Consume;
import com.example.rfidserver.pojo.Ticket;
import com.example.rfidserver.pojo.User;
import com.example.rfidserver.status.FailActivity;
import com.example.rfidserver.status.SuccessActivity;

public class DialogHelper {
    /**
     * 普通对话框
     *
     * 对话框中的一些属性不一定是必须的，比如可以不设置标题、图标之类的，
     *
     * 也可以只设置一个按钮或者两个按钮，如常见的确认退出对话框
     */
    public static void commonDialog(Context context,String titile,String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titile);// 设置标题
        // builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setMessage(msg);// 为对话框设置内容
        // 为对话框设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("取消");
            }
        });
        // 为对话框设置中立按钮
        // builder.setNeutralButton("中立", new OnClickListener() {
        //
        // @Override
        // public void onClick(DialogInterface arg0, int arg1) {
        // // TODO Auto-generated method stub
        // myToast("您点击了中立按钮");
        // }
        // });
        // 为对话框设置确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("确定");
            }
        });
        builder.create().show();// 使用show()方法显示对话框
    }

    public static void commonDialog(final Context context, String titile, String msg, final Ticket ticket) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titile);// 设置标题
        // builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setMessage(msg);// 为对话框设置内容
        // 为对话框设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("取消");
            }
        });
        // 为对话框设置中立按钮
        // builder.setNeutralButton("中立", new OnClickListener() {
        //
        // @Override
        // public void onClick(DialogInterface arg0, int arg1) {
        // // TODO Auto-generated method stub
        // myToast("您点击了中立按钮");
        // }
        // });
        // 为对话框设置确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("确定");
                TicketDao ticketDao = new TicketDao();
                int i = ticketDao.deleteOneTicketByID(ticket.getTicket_id());
                if(i!=0){
                    Intent intent = new Intent(context, SuccessActivity.class);
                    context.startActivity(intent);
                }else{
                    Intent intent = new Intent(context, FailActivity.class);
                    context.startActivity(intent);
                }
            }
        });
        builder.create().show();// 使用show()方法显示对话框
    }


    public static void commonDialog(final Context context, String titile, String msg, final Consume consume) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titile);// 设置标题
        // builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setMessage(msg);// 为对话框设置内容
        // 为对话框设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("取消");
            }
        });
        // 为对话框设置中立按钮
        // builder.setNeutralButton("中立", new OnClickListener() {
        //
        // @Override
        // public void onClick(DialogInterface arg0, int arg1) {
        // // TODO Auto-generated method stub
        // myToast("您点击了中立按钮");
        // }
        // });
        // 为对话框设置确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("确定");
                ConsumeDao consumeDao = new ConsumeDao();
                int i = consumeDao.deleteOneServicetByID(consume.getConsume_id());
                if(i!=0){
                    Intent intent = new Intent(context, SuccessActivity.class);
                    context.startActivity(intent);
                }else{
                    Intent intent = new Intent(context, FailActivity.class);
                    context.startActivity(intent);
                }
            }
        });
        builder.create().show();// 使用show()方法显示对话框
    }


    public static void commonDialog(final Context context, String titile, String msg, final User user) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titile);// 设置标题
        // builder.setIcon(R.drawable.ic_launcher);//设置图标
        builder.setMessage(msg);// 为对话框设置内容
        // 为对话框设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("取消");
            }
        });
        // 为对话框设置中立按钮
        // builder.setNeutralButton("中立", new OnClickListener() {
        //
        // @Override
        // public void onClick(DialogInterface arg0, int arg1) {
        // // TODO Auto-generated method stub
        // myToast("您点击了中立按钮");
        // }
        // });
        // 为对话框设置确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                System.out.println("确定");
                UserDao userDao = new UserDao();
                int i = userDao.deleteUserByID(user.getId());
                if(i!=0){
                    Intent intent = new Intent(context, SuccessActivity.class);
                    context.startActivity(intent);
                }else{
                    Intent intent = new Intent(context, FailActivity.class);
                    context.startActivity(intent);
                }
            }
        });
        builder.create().show();// 使用show()方法显示对话框
    }


    /**
     * 自定义布局的对话框,
     * 更新对话框
     */


    public static void layDialog(final Context context, final Ticket ticket) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        // 创建一个view，并且将布局加入view中
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_edt, null, false);
        // 将view添加到builder中
        builder.setView(view);
        // 创建dialog
        final Dialog dialog = builder.create();
        // 初始化控件，注意这里是通过view.findViewById
        final TextView tv1 = (TextView) view.findViewById(R.id.dialog_edit_tv1);
        final TextView tv2 = (TextView) view.findViewById(R.id.dialog_edit_tv2);
        final EditText edt1 = (EditText) view.findViewById(R.id.dialog_edit_et1);
        final EditText edt2 = (EditText) view.findViewById(R.id.dialog_edit_et2);
        tv1.setText("门票类型");
        tv2.setText("门票描述");

        final Button confirm = (Button) view.findViewById(R.id.confirm);
        Button cancel = (Button) view.findViewById(R.id.cancel);
        //先塞进去，放置一会更新数据时为空
        edt1.setText(ticket.getTicket_type());
        edt2.setText(ticket.getTicket_description());
        // 设置button的点击事件及获取editview中的文本内容
        confirm.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String ticket_type = edt1.getText().toString();
                String ticket_desp = edt2.getText().toString();
                Ticket update_ticket = new Ticket();
                update_ticket.setTicket_id(ticket.getTicket_id());
                update_ticket.setTicket_type(ticket_type);
                update_ticket.setTicket_description(ticket_desp);
                TicketDao ticketDao = new TicketDao();
                int i = ticketDao.updateOneTicketByID(update_ticket);
                System.out.println("TICAKetDAO::"+update_ticket);
                //更新成功
                if(i!=0){
                    Intent intent = new Intent(context,SuccessActivity.class);
                    context.startActivity(intent);
                }else{
                    Intent intent = new Intent(context,FailActivity.class);
                    context.startActivity(intent);
                }

            }
        });
        // 取消按钮
        cancel.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public static void  layDialog(final Context context, final Consume consume) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        // 创建一个view，并且将布局加入view中
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_edt, null, false);
        // 将view添加到builder中
        builder.setView(view);
        // 创建dialog
        final Dialog dialog = builder.create();
        // 初始化控件，注意这里是通过view.findViewById
        final TextView tv1 = (TextView) view.findViewById(R.id.dialog_edit_tv1);
        final TextView tv2 = (TextView) view.findViewById(R.id.dialog_edit_tv2);
        final EditText edt1 = (EditText) view.findViewById(R.id.dialog_edit_et1);
        final EditText edt2 = (EditText) view.findViewById(R.id.dialog_edit_et2);
        tv1.setText("消费项名");
        tv2.setText("消费项积分");
        final Button confirm = (Button) view.findViewById(R.id.confirm);
        Button cancel = (Button) view.findViewById(R.id.cancel);
        //先塞进去，放置一会更新数据时为空
        edt1.setText(consume.getConsume_Item_name());
        edt2.setText(String.valueOf(consume.getConsume_Item_integral()));
        // 设置button的点击事件及获取editview中的文本内容
        confirm.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String consume_item_name = edt1.getText().toString();
                String consume_item_integral = edt2.getText().toString();
                Consume update_consume = new Consume();
                update_consume.setConsume_id(consume.getConsume_id());
                update_consume.setConsume_Item_name(consume_item_name);
                update_consume.setConsume_Item_integral(Double.valueOf(consume_item_integral));
                ConsumeDao consumeDao = new ConsumeDao();
                System.out.println("ConSumeDAO::"+consumeDao);
                int i = consumeDao.updateOneServiceByID(update_consume);
                //更新成功
                if(i!=0){
                    Intent intent = new Intent(context,SuccessActivity.class);
                    context.startActivity(intent);
                }else{
                    Intent intent = new Intent(context,FailActivity.class);
                    context.startActivity(intent);
                }
            }
        });
        // 取消按钮
        cancel.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }




}
